home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / teapot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  15.9 KB  |  656 lines  |  [TEXT/MMCC]

  1. /*
  2.  * teapot.c - Tessellate the famous teapot into triangular patches, and sit the
  3.  *    object on top of a checkerboard which is meshed to the same degree.  In
  4.  *    other words, the number of squares on the checkerboard is the same as
  5.  *    the amount of meshing for each of the 32 patches on the teapot, with
  6.  *    each mesh quadrilateral on the teapot further split into two triangles.
  7.  *    Two light sources.
  8.  *
  9.  *    Note that the teapot should always be rendered as a double sided object
  10.  *    (since for some patches both sides can be seen).  (See IEEE CG&A
  11.  *    January 1987 for a history of the teapot.  Note that their vertex list
  12.  *    has duplicates, e.g. 93 and 271, as well as unused vertices, e.g.
  13.  *    205,206,216,223).  Also, note that the bottom (the last four patches)
  14.  *    is not flat - blame Frank Crow, not me.
  15.  *
  16.  * Author:  Eric Haines, 3D/Eye, Inc.
  17.  *
  18.  * size_factor determines the number of objects output.
  19.  *    Total patches = 32*2*n*n - 8*n     [degenerates are deleted]
  20.  *    Total squares = n*n
  21.  *
  22.  *    size_factor    # patches    # squares
  23.  *         1            56             1        [has more degenerates!]
  24.  *         2           240             4
  25.  *         3           552             9
  26.  *
  27.  *        12          9120           144
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <stdlib.h>    /* atoi */
  33. #include "def.h"
  34. #include "drv.h"    /* display_close() */
  35. #include "lib.h"
  36.  
  37. static int size_factor = 6 ;
  38. static int raytracer_format = OUTPUT_RT_DEFAULT;
  39. static int output_format    = OUTPUT_CURVES;
  40.  
  41.  
  42. /* comment out the next line to generate all patches except the bottom,
  43.  * i.e. the original Newell teapot
  44.  */
  45. #define BOTTOM
  46.  
  47. #ifdef    BOTTOM
  48. #define    NUM_PATCHES    32
  49. #else
  50. #define    NUM_PATCHES    28
  51. #endif
  52.  
  53.  
  54. static    int    Patches[32][4][4] = {
  55. /* rim */
  56. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  57. 3, 16, 17, 18, 7, 19, 20, 21, 11, 22, 23, 24, 15, 25, 26, 27,
  58. 18, 28, 29, 30, 21, 31, 32, 33, 24, 34, 35, 36, 27, 37, 38, 39,
  59. 30, 40, 41, 0, 33, 42, 43, 4, 36, 44, 45, 8, 39, 46, 47, 12,
  60. /* body */
  61. 12, 13, 14, 15, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  62. 15, 25, 26, 27, 51, 60, 61, 62, 55, 63, 64, 65, 59, 66, 67, 68,
  63. 27, 37, 38, 39, 62, 69, 70, 71, 65, 72, 73, 74, 68, 75, 76, 77,
  64. 39, 46, 47, 12, 71, 78, 79, 48, 74, 80, 81, 52, 77, 82, 83, 56,
  65. 56, 57, 58, 59, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  66. 59, 66, 67, 68, 87, 96, 97, 98, 91, 99, 100, 101, 95, 102, 103, 104,
  67. 68, 75, 76, 77, 98, 105, 106, 107, 101, 108, 109, 110, 104, 111, 112, 113,
  68. 77, 82, 83, 56, 107, 114, 115, 84, 110, 116, 117, 88, 113, 118, 119, 92,
  69. /* handle */
  70. 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
  71. 123, 136, 137, 120, 127, 138, 139, 124, 131, 140, 141, 128, 135, 142, 143, 132,
  72. 132, 133, 134, 135, 144, 145, 146, 147, 148, 149, 150, 151, 68, 152, 153, 154,
  73. 135, 142, 143, 132, 147, 155, 156, 144, 151, 157, 158, 148, 154, 159, 160, 68,
  74. /* spout */
  75. 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
  76. 164, 177, 178, 161, 168, 179, 180, 165, 172, 181, 182, 169, 176, 183, 184, 173,
  77. 173, 174, 175, 176, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
  78. 176, 183, 184, 173, 188, 197, 198, 185, 192, 199, 200, 189, 196, 201, 202, 193,
  79. /* lid */
  80. 203, 203, 203, 203, 204, 205, 206, 207, 208, 208, 208, 208, 209, 210, 211, 212,
  81. 203, 203, 203, 203, 207, 213, 214, 215, 208, 208, 208, 208, 212, 216, 217, 218,
  82. 203, 203, 203, 203, 215, 219, 220, 221, 208, 208, 208, 208, 218, 222, 223, 224,
  83. 203, 203, 203, 203, 221, 225, 226, 204, 208, 208, 208, 208, 224, 227, 228, 209,
  84. 209, 210, 211, 212, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
  85. 212, 216, 217, 218, 232, 241, 242, 243, 236, 244, 245, 246, 240, 247, 248, 249,
  86. 218, 222, 223, 224, 243, 250, 251, 252, 246, 253, 254, 255, 249, 256, 257, 258,
  87. 224, 227, 228, 209, 252, 259, 260, 229, 255, 261, 262, 233, 258, 263, 264, 237,
  88. /* bottom */
  89. 265, 265, 265, 265, 266, 267, 268, 269, 270, 271, 272, 273, 92, 119, 118, 113,
  90. 265, 265, 265, 265, 269, 274, 275, 276, 273, 277, 278, 279, 113, 112, 111, 104,
  91. 265, 265, 265, 265, 276, 280, 281, 282, 279, 283, 284, 285, 104, 103, 102, 95,
  92. 265, 265, 265, 265, 282, 286, 287, 266, 285, 288, 289, 270, 95, 94, 93, 92
  93. } ;
  94.  
  95. static    COORD3    Verts[290] = {
  96.  1.4, 0, 2.4,
  97.  1.4, -0.784, 2.4,
  98.  0.784, -1.4, 2.4,
  99.  0, -1.4, 2.4,
  100.  1.3375, 0, 2.53125,
  101.  1.3375, -0.749, 2.53125,
  102.  0.749, -1.3375, 2.53125,
  103.  0, -1.3375, 2.53125,
  104.  1.4375, 0, 2.53125,
  105.  1.4375, -0.805, 2.53125,
  106.  0.805, -1.4375, 2.53125,
  107.  0, -1.4375, 2.53125,
  108.  1.5, 0, 2.4,
  109.  1.5, -0.84, 2.4,
  110.  0.84, -1.5, 2.4,
  111.  0, -1.5, 2.4,
  112.  -0.784, -1.4, 2.4,
  113.  -1.4, -0.784, 2.4,
  114.  -1.4, 0, 2.4,
  115.  -0.749, -1.3375, 2.53125,
  116.  -1.3375, -0.749, 2.53125,
  117.  -1.3375, 0, 2.53125,
  118.  -0.805, -1.4375, 2.53125,
  119.  -1.4375, -0.805, 2.53125,
  120.  -1.4375, 0, 2.53125,
  121.  -0.84, -1.5, 2.4,
  122.  -1.5, -0.84, 2.4,
  123.  -1.5, 0, 2.4,
  124.  -1.4, 0.784, 2.4,
  125.  -0.784, 1.4, 2.4,
  126.  0, 1.4, 2.4,
  127.  -1.3375, 0.749, 2.53125,
  128.  -0.749, 1.3375, 2.53125,
  129.  0, 1.3375, 2.53125,
  130.  -1.4375, 0.805, 2.53125,
  131.  -0.805, 1.4375, 2.53125,
  132.  0, 1.4375, 2.53125,
  133.  -1.5, 0.84, 2.4,
  134.  -0.84, 1.5, 2.4,
  135.  0, 1.5, 2.4,
  136.  0.784, 1.4, 2.4,
  137.  1.4, 0.784, 2.4,
  138.  0.749, 1.3375, 2.53125,
  139.  1.3375, 0.749, 2.53125,
  140.  0.805, 1.4375, 2.53125,
  141.  1.4375, 0.805, 2.53125,
  142.  0.84, 1.5, 2.4,
  143.  1.5, 0.84, 2.4,
  144.  1.75, 0, 1.875,
  145.  1.75, -0.98, 1.875,
  146.  0.98, -1.75, 1.875,
  147.  0, -1.75, 1.875,
  148.  2, 0, 1.35,
  149.  2, -1.12, 1.35,
  150.  1.12, -2, 1.35,
  151.  0, -2, 1.35,
  152.  2, 0, 0.9,
  153.  2, -1.12, 0.9,
  154.  1.12, -2, 0.9,
  155.  0, -2, 0.9,
  156.  -0.98, -1.75, 1.875,
  157.  -1.75, -0.98, 1.875,
  158.  -1.75, 0, 1.875,
  159.  -1.12, -2, 1.35,
  160.  -2, -1.12, 1.35,
  161.  -2, 0, 1.35,
  162.  -1.12, -2, 0.9,
  163.  -2, -1.12, 0.9,
  164.  -2, 0, 0.9,
  165.  -1.75, 0.98, 1.875,
  166.  -0.98, 1.75, 1.875,
  167.  0, 1.75, 1.875,
  168.  -2, 1.12, 1.35,
  169.  -1.12, 2, 1.35,
  170.  0, 2, 1.35,
  171.  -2, 1.12, 0.9,
  172.  -1.12, 2, 0.9,
  173.  0, 2, 0.9,
  174.  0.98, 1.75, 1.875,
  175.  1.75, 0.98, 1.875,
  176.  1.12, 2, 1.35,
  177.  2, 1.12, 1.35,
  178.  1.12, 2, 0.9,
  179.  2, 1.12, 0.9,
  180.  2, 0, 0.45,
  181.  2, -1.12, 0.45,
  182.  1.12, -2, 0.45,
  183.  0, -2, 0.45,
  184.  1.5, 0, 0.225,
  185.  1.5, -0.84, 0.225,
  186.  0.84, -1.5, 0.225,
  187.  0, -1.5, 0.225,
  188.  1.5, 0, 0.15,
  189.  1.5, -0.84, 0.15,
  190.  0.84, -1.5, 0.15,
  191.  0, -1.5, 0.15,
  192.  -1.12, -2, 0.45,
  193.  -2, -1.12, 0.45,
  194.  -2, 0, 0.45,
  195.  -0.84, -1.5, 0.225,
  196.  -1.5, -0.84, 0.225,
  197.  -1.5, 0, 0.225,
  198.  -0.84, -1.5, 0.15,
  199.  -1.5, -0.84, 0.15,
  200.  -1.5, 0, 0.15,
  201.  -2, 1.12, 0.45,
  202.  -1.12, 2, 0.45,
  203.  0, 2, 0.45,
  204.  -1.5, 0.84, 0.225,
  205.  -0.84, 1.5, 0.225,
  206.  0, 1.5, 0.225,
  207.  -1.5, 0.84, 0.15,
  208.  -0.84, 1.5, 0.15,
  209.  0, 1.5, 0.15,
  210.  1.12, 2, 0.45,
  211.  2, 1.12, 0.45,
  212.  0.84, 1.5, 0.225,
  213.  1.5, 0.84, 0.225,
  214.  0.84, 1.5, 0.15,
  215.  1.5, 0.84, 0.15,
  216.  -1.6, 0, 2.025,
  217.  -1.6, -0.3, 2.025,
  218.  -1.5, -0.3, 2.25,
  219.  -1.5, 0, 2.25,
  220.  -2.3, 0, 2.025,
  221.  -2.3, -0.3, 2.025,
  222.  -2.5, -0.3, 2.25,
  223.  -2.5, 0, 2.25,
  224.  -2.7, 0, 2.025,
  225.  -2.7, -0.3, 2.025,
  226.  -3, -0.3, 2.25,
  227.  -3, 0, 2.25,
  228.  -2.7, 0, 1.8,
  229.  -2.7, -0.3, 1.8,
  230.  -3, -0.3, 1.8,
  231.  -3, 0, 1.8,
  232.  -1.5, 0.3, 2.25,
  233.  -1.6, 0.3, 2.025,
  234.  -2.5, 0.3, 2.25,
  235.  -2.3, 0.3, 2.025,
  236.  -3, 0.3, 2.25,
  237.  -2.7, 0.3, 2.025,
  238.  -3, 0.3, 1.8,
  239.  -2.7, 0.3, 1.8,
  240.  -2.7, 0, 1.575,
  241.  -2.7, -0.3, 1.575,
  242.  -3, -0.3, 1.35,
  243.  -3, 0, 1.35,
  244.  -2.5, 0, 1.125,
  245.  -2.5, -0.3, 1.125,
  246.  -2.65, -0.3, 0.9375,
  247.  -2.65, 0, 0.9375,
  248.  -2, -0.3, 0.9,
  249.  -1.9, -0.3, 0.6,
  250.  -1.9, 0, 0.6,
  251.  -3, 0.3, 1.35,
  252.  -2.7, 0.3, 1.575,
  253.  -2.65, 0.3, 0.9375,
  254.  -2.5, 0.3, 1.125,
  255.  -1.9, 0.3, 0.6,
  256.  -2, 0.3, 0.9,
  257.  1.7, 0, 1.425,
  258.  1.7, -0.66, 1.425,
  259.  1.7, -0.66, 0.6,
  260.  1.7, 0, 0.6,
  261.  2.6, 0, 1.425,
  262.  2.6, -0.66, 1.425,
  263.  3.1, -0.66, 0.825,
  264.  3.1, 0, 0.825,
  265.  2.3, 0, 2.1,
  266.  2.3, -0.25, 2.1,
  267.  2.4, -0.25, 2.025,
  268.  2.4, 0, 2.025,
  269.  2.7, 0, 2.4,
  270.  2.7, -0.25, 2.4,
  271.  3.3, -0.25, 2.4,
  272.  3.3, 0, 2.4,
  273.  1.7, 0.66, 0.6,
  274.  1.7, 0.66, 1.425,
  275.  3.1, 0.66, 0.825,
  276.  2.6, 0.66, 1.425,
  277.  2.4, 0.25, 2.025,
  278.  2.3, 0.25, 2.1,
  279.  3.3, 0.25, 2.4,
  280.  2.7, 0.25, 2.4,
  281.  2.8, 0, 2.475,
  282.  2.8, -0.25, 2.475,
  283.  3.525, -0.25, 2.49375,
  284.  3.525, 0, 2.49375,
  285.  2.9, 0, 2.475,
  286.  2.9, -0.15, 2.475,
  287.  3.45, -0.15, 2.5125,
  288.  3.45, 0, 2.5125,
  289.  2.8, 0, 2.4,
  290.  2.8, -0.15, 2.4,
  291.  3.2, -0.15, 2.4,
  292.  3.2, 0, 2.4,
  293.  3.525, 0.25, 2.49375,
  294.  2.8, 0.25, 2.475,
  295.  3.45, 0.15, 2.5125,
  296.  2.9, 0.15, 2.475,
  297.  3.2, 0.15, 2.4,
  298.  2.8, 0.15, 2.4,
  299.  0, 0, 3.15,
  300.  0.8, 0, 3.15,
  301.  0.8, -0.45, 3.15,
  302.  0.45, -0.8, 3.15,
  303.  0, -0.8, 3.15,
  304.  0, 0, 2.85,
  305.  0.2, 0, 2.7,
  306.  0.2, -0.112, 2.7,
  307.  0.112, -0.2, 2.7,
  308.  0, -0.2, 2.7,
  309.  -0.45, -0.8, 3.15,
  310.  -0.8, -0.45, 3.15,
  311.  -0.8, 0, 3.15,
  312.  -0.112, -0.2, 2.7,
  313.  -0.2, -0.112, 2.7,
  314.  -0.2, 0, 2.7,
  315.  -0.8, 0.45, 3.15,
  316.  -0.45, 0.8, 3.15,
  317.  0, 0.8, 3.15,
  318.  -0.2, 0.112, 2.7,
  319.  -0.112, 0.2, 2.7,
  320.  0, 0.2, 2.7,
  321.  0.45, 0.8, 3.15,
  322.  0.8, 0.45, 3.15,
  323.  0.112, 0.2, 2.7,
  324.  0.2, 0.112, 2.7,
  325.  0.4, 0, 2.55,
  326.  0.4, -0.224, 2.55,
  327.  0.224, -0.4, 2.55,
  328.  0, -0.4, 2.55,
  329.  1.3, 0, 2.55,
  330.  1.3, -0.728, 2.55,
  331.  0.728, -1.3, 2.55,
  332.  0, -1.3, 2.55,
  333.  1.3, 0, 2.4,
  334.  1.3, -0.728, 2.4,
  335.  0.728, -1.3, 2.4,
  336.  0, -1.3, 2.4,
  337.  -0.224, -0.4, 2.55,
  338.  -0.4, -0.224, 2.55,
  339.  -0.4, 0, 2.55,
  340.  -0.728, -1.3, 2.55,
  341.  -1.3, -0.728, 2.55,
  342.  -1.3, 0, 2.55,
  343.  -0.728, -1.3, 2.4,
  344.  -1.3, -0.728, 2.4,
  345.  -1.3, 0, 2.4,
  346.  -0.4, 0.224, 2.55,
  347.  -0.224, 0.4, 2.55,
  348.  0, 0.4, 2.55,
  349.  -1.3, 0.728, 2.55,
  350.  -0.728, 1.3, 2.55,
  351.  0, 1.3, 2.55,
  352.  -1.3, 0.728, 2.4,
  353.  -0.728, 1.3, 2.4,
  354.  0, 1.3, 2.4,
  355.  0.224, 0.4, 2.55,
  356.  0.4, 0.224, 2.55,
  357.  0.728, 1.3, 2.55,
  358.  1.3, 0.728, 2.55,
  359.  0.728, 1.3, 2.4,
  360.  1.3, 0.728, 2.4,
  361.  0, 0, 0,
  362.  1.425, 0, 0,
  363.  1.425, 0.798, 0,
  364.  0.798, 1.425, 0,
  365.  0, 1.425, 0,
  366.  1.5, 0, 0.075,
  367.  1.5, 0.84, 0.075,
  368.  0.84, 1.5, 0.075,
  369.  0, 1.5, 0.075,
  370.  -0.798, 1.425, 0,
  371.  -1.425, 0.798, 0,
  372.  -1.425, 0, 0,
  373.  -0.84, 1.5, 0.075,
  374.  -1.5, 0.84, 0.075,
  375.  -1.5, 0, 0.075,
  376.  -1.425, -0.798, 0,
  377.  -0.798, -1.425, 0,
  378.  0, -1.425, 0,
  379.  -1.5, -0.84, 0.075,
  380.  -0.84, -1.5, 0.075,
  381.  0, -1.5, 0.075,
  382.  0.798, -1.425, 0,
  383.  1.425, -0.798, 0,
  384.  0.84, -1.5, 0.075,
  385.  1.5, -0.84, 0.075
  386. } ;
  387.  
  388.  
  389. /* at the center of the lid's handle and at bottom are cusp points -
  390.  * check if normal is (0 0 0), if so, check that polygon is not degenerate.
  391.  * If degenerate, return FALSE, else set normal.
  392.  */
  393. int    check_for_cusp( tot_vert, vert, norm )
  394. int    tot_vert ;
  395. COORD3    vert[] ;
  396. COORD3    norm[] ;
  397. {
  398. int    count, i, nv ;
  399.  
  400.     for ( count = 0, i = tot_vert ; i-- ; ) {
  401.     /* check if vertex is at cusp */
  402.     if ( IS_VAL_ALMOST_ZERO( vert[i][X], 0.0001 ) &&
  403.          IS_VAL_ALMOST_ZERO( vert[i][Y], 0.0001 ) ) {
  404.         count++ ;
  405.         nv = i ;
  406.     }
  407.     }
  408.  
  409.     if ( count > 1 ) {
  410.     /* degenerate */
  411.     return( FALSE ) ;
  412.     }
  413.     if ( count == 1 ) {
  414.     /* check if point is somewhere above the middle of the teapot */
  415.     if ( vert[nv][Z] > 1.5 ) {
  416.         /* cusp at lid */
  417.         SET_COORD3( norm[nv], 0.0, 0.0, 1.0 ) ;
  418.     } else {
  419.         /* cusp at bottom */
  420.         SET_COORD3( norm[nv], 0.0, 0.0, -1.0 ) ;
  421.     }
  422.     }
  423.     return( TRUE ) ;
  424. }
  425.  
  426.  
  427. void
  428. points_from_basis( tot_vert, s, t, mgm, vert, norm )
  429. int    tot_vert ;
  430. double    s[] ;
  431. double    t[] ;
  432. MATRIX    mgm[3] ;
  433. COORD3    vert[] ;
  434. COORD3    norm[] ;
  435. {
  436. int    i, num_vert, p ;
  437. double    sval, tval, dsval, dtval, sxyz, txyz ;
  438. COORD3    sdir, tdir ;
  439. COORD4    sp, tp, dsp, dtp, tcoord ;
  440.  
  441.     for ( num_vert = 0 ; num_vert < tot_vert ; num_vert++ ) {
  442.  
  443.     sxyz = s[num_vert] ;
  444.     txyz = t[num_vert] ;
  445.  
  446.     /* get power vectors and their derivatives */
  447.     for ( p = 4, sval = tval = 1.0 ; p-- ; ) {
  448.         sp[p] = sval ;
  449.         tp[p] = tval ;
  450.         sval *= sxyz ;
  451.         tval *= txyz ;
  452.  
  453.         if ( p == 3 ) {
  454.         dsp[p] = dtp[p] = 0.0 ;
  455.         dsval = dtval = 1.0 ;
  456.         } else {
  457.         dsp[p] = dsval * (double)(3-p) ;
  458.         dtp[p] = dtval * (double)(3-p) ;
  459.         dsval *= sxyz ;
  460.         dtval *= txyz ;
  461.         }
  462.     }
  463.  
  464.     /* do for x,y,z */
  465.     for ( i = 0 ; i < 3 ; i++ ) {
  466.         /* multiply power vectors times matrix to get value */
  467.         lib_transform_coord( tcoord, sp, mgm[i] ) ;
  468.         vert[num_vert][i] = DOT4( tcoord, tp ) ;
  469.  
  470.         /* get s and t tangent vectors */
  471.         lib_transform_coord( tcoord, dsp, mgm[i] ) ;
  472.         sdir[i] = DOT4( tcoord, tp ) ;
  473.  
  474.         lib_transform_coord( tcoord, sp, mgm[i] ) ;
  475.         tdir[i] = DOT4( tcoord, dtp ) ;
  476.     }
  477.  
  478.     /* find normal */
  479.     CROSS( norm[num_vert], tdir, sdir ) ;
  480.     (void)lib_normalize_vector( norm[num_vert] ) ;
  481.     }
  482. }
  483.  
  484. /* Compute points on each spline surface of teapot by brute force.
  485.  * Forward differencing would be faster, but this is compact & simple.
  486.  */
  487. void
  488. output_teapot()
  489. {
  490. /* bezier form */
  491. static MATRIX ms = { -1.0,  3.0, -3.0,  1.0,
  492.               3.0, -6.0,  3.0,  0.0,
  493.              -3.0,  3.0,  0.0,  0.0,
  494.               1.0,  0.0,  0.0,  0.0 } ;
  495. int    surf, i, r, c, sstep, tstep, num_tri, num_vert, num_tri_vert ;
  496. double    s[3], t[3] ;
  497. COORD3    vert[4], norm[4] ;
  498. COORD3    obj_color ;
  499. MATRIX    mst, g, mgm[3], tmtx ;
  500.  
  501.     SET_COORD3( obj_color, 1.0, 0.5, 0.1 ) ;
  502.     lib_output_color(NULL, obj_color, 0.0, 0.75, 0.25, 0.25, 5.0, 0.0, 0.0 ) ;
  503.  
  504.     lib_transpose_matrix( mst, ms ) ;
  505.  
  506.     for ( surf = 0 ; surf < NUM_PATCHES ; surf++ ) {
  507.  
  508.     /* get M * G * M matrix for x,y,z */
  509.     for ( i = 0 ; i < 3 ; i++ ) {
  510.         /* get control patches */
  511.         for ( r = 0 ; r < 4 ; r++ ) {
  512.         for ( c = 0 ; c < 4 ; c++ ) {
  513.             g[r][c] = Verts[Patches[surf][r][c]][i] ;
  514.         }
  515.         }
  516.  
  517.         lib_matrix_multiply( tmtx, ms, g ) ;
  518.         lib_matrix_multiply( mgm[i], tmtx, mst ) ;
  519.     }
  520.  
  521.     /* step along, get points, and output */
  522.     for ( sstep = 0 ; sstep < size_factor ; sstep++ ) {
  523.         for ( tstep = 0 ; tstep < size_factor ; tstep++ ) {
  524.         for ( num_tri = 0 ; num_tri < 2 ; num_tri++ ) {
  525.             for ( num_vert = 0 ; num_vert < 3 ; num_vert++ ) {
  526.             num_tri_vert = ( num_vert + num_tri * 2 ) % 4 ;
  527.             /* trickiness: add 1 to sstep if 1 or 2 */
  528.             s[num_vert] = (double)(sstep + (num_tri_vert/2 ? 1:0) )
  529.                     / (double)size_factor ;
  530.             /* trickiness: add 1 to tstep if 2 or 3 */
  531.             t[num_vert] = (double)(tstep + (num_tri_vert%3 ? 1:0) )
  532.                     / (double)size_factor ;
  533.             }
  534.             points_from_basis( 3, s, t, mgm, vert, norm ) ;
  535.             /* don't output degenerate polygons */
  536.             if ( check_for_cusp( 3, vert, norm ) ) {
  537.             lib_output_polypatch( 3, vert, norm ) ;
  538.             PLATFORM_MULTITASK();
  539.             }
  540.         }
  541.         }
  542.     }
  543.     }
  544. }
  545.  
  546. void
  547. loc_to_square( sstep, tstep, vert )
  548. int    sstep ;
  549. int    tstep ;
  550. COORD3    vert[4] ;
  551. {
  552. int    i ;
  553.  
  554.     for ( i = 0 ; i < 4 ; i++ ) {
  555.     /* vertex 0 & 3 are x.low, 1 & 2 are x.high */
  556.     vert[i][X] = 4.0 * ( 2.0 * (double)(sstep + (i%3 ? 1 : 0) ) /
  557.         (double)size_factor - 1.0 ) ;
  558.  
  559.     /* vertex 0 & 1 are y.low, 2 & 3 are y.high */
  560.     vert[i][Y] = 4.0 * ( 2.0 * (double)(tstep + (i/2 ? 1 : 0) ) /
  561.         (double)size_factor - 1.0 ) ;
  562.  
  563.     vert[i][Z] = 0.0 ;
  564.     }
  565. }
  566.  
  567.  
  568. void
  569. output_checkerboard()
  570. {
  571. int    sstep, tstep ;
  572. COORD3    vert[4] ;
  573. COORD3    obj_color ;
  574.  
  575.     SET_COORD3( obj_color, 1.0, 1.0, 1.0 ) ;
  576.     lib_output_color(NULL, obj_color, 0.0, 0.5, 0.5, 0.5, 15.0, 0.0, 0.0 ) ;
  577.     for ( sstep = 0 ; sstep < size_factor ; sstep++ ) {
  578.     for ( tstep = 0 ; tstep < size_factor ; tstep++ ) {
  579.         if ( ( sstep + tstep ) % 2 ) {
  580.         loc_to_square( sstep, tstep, vert ) ;
  581.         lib_output_polygon( 4, vert ) ;
  582.         }
  583.     }
  584.     }
  585.  
  586.     SET_COORD3( obj_color, 0.5, 0.5, 0.5 ) ;
  587.     lib_output_color(NULL, obj_color, 0.0, 0.5, 0.5, 0.5, 15.0, 0.0, 0.0 ) ;
  588.     for ( sstep = 0 ; sstep < size_factor ; sstep++ ) {
  589.     for ( tstep = 0 ; tstep < size_factor ; tstep++ ) {
  590.         if ( !(( sstep + tstep ) % 2) ) {
  591.         loc_to_square( sstep, tstep, vert ) ;
  592.         lib_output_polygon( 4, vert ) ;
  593.         }
  594.     }
  595.     }
  596. }
  597.  
  598.  
  599. int
  600. main(argc,argv)
  601.     int argc;
  602.     char *argv[];
  603. {
  604.     double lscale;
  605.     COORD3 back_color;
  606.     COORD3 from, at, up;
  607.     COORD4 light;
  608.  
  609.     PLATFORM_INIT(SPD_TEAPOT);
  610.  
  611.     /* Start by defining which raytracer we will be using */
  612.     if ( lib_gen_get_opts( argc, argv,
  613.             &size_factor, &raytracer_format, &output_format ) ) {
  614.     return EXIT_FAIL;
  615.     }
  616.     if ( lib_open( raytracer_format, "Teapot.out" ) ) {
  617.     return EXIT_FAIL;
  618.     }
  619.  
  620.     lib_set_polygonalization(3, 3);
  621.     if ( size_factor == 1 ) {
  622.     fprintf(stderr,
  623.       "warning: a size of 1 is not supported - use at your own risk\n" ) ;
  624.     }
  625.  
  626.     /* output background color - UNC sky blue */
  627.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  628.     SET_COORD3( back_color, 0.078, 0.361, 0.753 ) ;
  629.     lib_output_background_color( back_color ) ;
  630.  
  631.     /* output viewpoint */
  632.     SET_COORD3( from, 4.86, 7.2, 5.4 ) ;
  633.     SET_COORD3( at, 0.0, 0.0, 0.0 ) ;
  634.     SET_COORD3( up, 0.0, 0.0, 1.0 ) ;
  635.     lib_output_viewpoint( from, at, up, 45.0, 1.0, 1.0, 512, 512 ) ;
  636.  
  637.     /* For raytracers that don't scale the light intensity, we will do
  638.        it for them */
  639.     lscale = (raytracer_format == OUTPUT_NFF ||
  640.           raytracer_format == OUTPUT_RTRACE ? 1.0 : 1.0 / sqrt(2.0));
  641.  
  642.     /* output light sources */
  643.     SET_COORD4( light, -3.1, 9.8, 12.1, lscale ) ;
  644.     lib_output_light( light ) ;
  645.     SET_COORD4( light, 11.3, 5.1, 8.8, lscale ) ;
  646.     lib_output_light( light ) ;
  647.  
  648.     output_checkerboard() ;
  649.     output_teapot() ;
  650.  
  651.     lib_close();
  652.  
  653.     PLATFORM_SHUTDOWN();
  654.     return EXIT_SUCCESS;
  655. }
  656.